package com.pacy.swj.swjwb;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gyf.immersionbar.ImmersionBar;
import com.pacy.swj.swjwb.Utils.Constans;
import com.pacy.swj.swjwb.Utils.DividerDecoration;
import com.pacy.swj.swjwb.Utils.HttpUtils;
import com.pacy.swj.swjwb.activitys.FinalListActivity;
import com.pacy.swj.swjwb.activitys.ReadActivity;
import com.pacy.swj.swjwb.activitys.SearchActivity;
import com.pacy.swj.swjwb.activitys.TypeListActivity;
import com.pacy.swj.swjwb.activitys.WebActivity;
import com.pacy.swj.swjwb.adapters.MainAdapter;
import com.pacy.swj.swjwb.beans.BaseBean;
import com.pacy.swj.swjwb.beans.QureyMapBean;
import com.pacy.swj.swjwb.views.HRecyclerView;

import org.xutils.common.Callback;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import h.framework.android.utils.JsonUtil;
import h.framework.android.utils.ToastUtil;

public class MainActivity extends AppCompatActivity {
    MainAdapter mAdapter;
    GridLayoutManager manager;
    @BindView(R.id.recyclerView)
    HRecyclerView recycler;

    View notDataView;
    View errorView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImmersionBar.with(this).init();
        ButterKnife.bind(this);
        initRecyclerView();
        sendRequest();

//        ReadActivity.show(MainActivity.this,"标题",Constans.DOWNLOAD_PATH+"test.doc");
    }


    /**
     * 初始化列表
     */
    private void initRecyclerView() {

        notDataView=getLayoutInflater().inflate(R.layout.empty_view, null, false);
        errorView=getLayoutInflater().inflate(R.layout.error_view, null, false);

        notDataView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendRequest();
            }
        });

        errorView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendRequest();
            }
        });

        mAdapter = new MainAdapter();
        manager = new GridLayoutManager(MainActivity.this, 3);
        DividerDecoration itemDecoration = new DividerDecoration(Color.TRANSPARENT, 20);
        recycler.addItemDecoration(itemDecoration);
        recycler.setLayoutManager(manager);
        recycler.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                sendRequest2(mAdapter.getItem(position).getMenuName(),mAdapter.getItem(position).getId());
            }
        });

    }

    @OnClick(value = {R.id.search_tv,R.id.search_lin})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_tv:
                Intent intent = new Intent(MainActivity.this, SearchActivity.class);
                intent.putExtra("content", "搜索的内容");
                startActivity(intent);
                break;

            case R.id.search_lin:
                Intent intent1 = new Intent(MainActivity.this, SearchActivity.class);
                intent1.putExtra("content", "搜索的内容");
                startActivity(intent1);
                break;
        }
    }


    /**
     * 发送请求
     */
    private void sendRequest2(final String MenuName, final long pid) {
        Map<String, Object> map = new HashMap<>();
        map.put("menu_Level", "2");
        map.put("menu_pid", pid);
        HttpUtils.Post(Constans.QUERYBYMAP, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.i("SWJ", result);
                BaseBean<List<QureyMapBean>> baseBean = JsonUtil.jsonToObject(result, new TypeToken<BaseBean<List<QureyMapBean>>>() {
                }.getType());
                if (baseBean.isSuccess()) {
                    if(baseBean.getData().size()>0){
                        Intent intent = new Intent(MainActivity.this, TypeListActivity.class);
                        intent.putExtra("title", MenuName);
                        intent.putExtra("id", pid);
                        intent.putExtra("right", "首页");
                        startActivity(intent);
                    }else{
                        Intent intent = new Intent(MainActivity.this, FinalListActivity.class);
                        intent.putExtra("title",MenuName);
                        intent.putExtra("id", pid);
                        intent.putExtra("right", "首页");
                        startActivity(intent);
                    }
                } else {
                    ToastUtil.error((String) baseBean.getMessage());
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i("SWJ", "请求出错---" + ex.toString());
                ToastUtil.error("请求失败,请重试");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    /**
     * 发送请求
     */
    private void sendRequest() {
        Map<String, Object> map = new HashMap<>();
        map.put("menu_Level", "1");
        HttpUtils.Post(Constans.QUERYBYMAP, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.i("SWJ", result);
                BaseBean<List<QureyMapBean>> baseBean = JsonUtil.jsonToObject(result, new TypeToken<BaseBean<List<QureyMapBean>>>() {
                }.getType());
                if (baseBean.isSuccess()) {
                    List<QureyMapBean> list=new ArrayList<>();
                    if(baseBean.getData().size()>0){
                        for(QureyMapBean qureyMapBean:baseBean.getData()){
                            if(qureyMapBean.getMenuStatus()==1){
                                list.add(qureyMapBean);
                            }
                        }
                    }
                    mAdapter.setEmptyView(notDataView);
                    mAdapter.setNewData(list);
                } else {
                    mAdapter.setEmptyView(errorView);
                    mAdapter.setNewData(null);
                    ToastUtil.error((String) baseBean.getMessage());
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i("SWJ", "请求出错---" + ex.toString());
                ToastUtil.error("请求失败,请重试");
                mAdapter.setEmptyView(errorView);
                mAdapter.setNewData(null);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });


    }

}
